<?php
/**
 * Created by PhpStorm.
 * User: henryxi-mac
 * Date: 2015/5/20
 * Time: 9:46
 */

error_reporting(E_ALL & ~E_NOTICE);
require_once dirname(__FILE__)."/lib/common.php";
require_once dirname(__FILE__)."/config.php";
global $db_config;
$db = new Mysql($db_config);
$dt = date("Y-m-d H:i:s",time());

$xml = "<xml>
    <AppId><![CDATA[wx9dce312479a493dd]]></AppId>
    <Encrypt><![CDATA[Dp2SJNuy6ovy9RXDNYf+6xO++vpqb0PTWS8Q8hU5FK1WKvTlkArCGNiO7G0/PTMf7bNE8ulAQ+4sq1W5T6O4ygq/OHZvYPt5GbOk6ajmlwvPiXBCRuND4NodEsL8zGsVWl8T9c/hI/PupUk15fOodSDMYWFY1XvyacPYp9uBDd0a23W0uTA5k4UIos+4YNR3Al6avj+y945laf1R4y3HgKOh5eq34SEjlCFj/IlbR56fdz8pvwhor2qHaUMfZD0yPlpZmb3aq8UHEqmqFtrqXKp4CoM28cm28I5sGqO5z8KH6h94cWQvMTXr3I4C7bx3xcamsSImMs/ygA5KcR0bwUXUgiLNo0OkZZK/3w12EfLxp2x/dgwZjhTcM056Z4SnCCVeND26yaCWL/VjVMhedZiSVDjelJOyz9i6vrtr/7KrTw965jBkDeYuV0ZcTaxcSO1UpnZxLlpjKNBzNKi8RQ==]]></Encrypt>
</xml>";
//{"signature":"35789d49615bd7490a738ff243624c81ac632ade","timestamp":"1432200014","nonce":"806618251","encrypt_type":"aes","msg_signature":"cf630bd7d6059e0dcd3e90a98a0e8c868f5e8071"}
//$xml = file_get_contents("test.xml");
$get = '{"signature":"edac0a98859da83e2f307a6f50c1f6145792f9ec","timestamp":"1432201814","nonce":"1723874288","encrypt_type":"aes","msg_signature":"b4ae80f3156150f5a490962c4e36ebb67a9049b1"}';
$get = json_decode($get,true);
//var_dump($get); exit();
include_once "wxBizMsgCrypt.php";
$xml_tree = new DOMDocument();
$xml_tree->loadXML($xml);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$encrypt = $array_e->item(0)->nodeValue;
//var_dump($xml_tree);exit();
// 第三方发送消息给公众平台
$encodingAesKey = "8w6wGsRXSaNwlxd51kWvSdgyNHK8C12XsFDD0aeyr3t";
$token = "350436_e";
$timeStamp = $get['timestamp'];
$nonce = $get['nonce'];
$appId = "wx9dce312479a493dd";
$msg_sign = $get['msg_signature'];
$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
$msg = '';
$errcode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $xml, $msg);
if ($errcode == 0){
    $xml_tree = new DOMDocument();
    $xml_tree->loadXML($msg);
    $array_t = $xml_tree->getElementsByTagName('ComponentVerifyTicket');
    $array_i = $xml_tree->getElementsByTagName('InfoType');
    $array_c = $xml_tree->getElementsByTagName('CreateTime');
    $ticket = $array_t->item(0)->nodeValue;
    $infoType = $array_i->item(0)->nodeValue;
    $createTime = $array_c->item(0)->nodeValue;

    $sql = "replace into authorize(`appid`,`VerifyTicket`,`InfoType`,`timestamp`) values('{$appId}','{$ticket}','{$infoType}','{$createTime}')";

    $db->sql($sql);
}
